/**
 * 
 */
package com.zxc.redis;

import java.util.ArrayList;
import java.util.HashSet;
import java.util.List;
import java.util.Set;
import java.util.concurrent.ExecutorService;
import java.util.concurrent.Executors;

import io.codis.jodis.JedisResourcePool;
import io.codis.jodis.RoundRobinJedisPool;
import redis.clients.jedis.Jedis;
import redis.clients.jedis.JedisPoolConfig;

/**
 * @author admin
 *
 */
public class CodisDemo {

	private static ExecutorService gdsThreadExecutor = Executors.newFixedThreadPool(500);
	
	public static void pushGdsLogTask(Runnable task){
		gdsThreadExecutor.submit(task);
	}
	
	/**
	 * @param args
	 */
	public static void main(String[] args) {
		JedisResourcePool jedisPool = buildPool();
		
//		JedisUtil.init();
//		JedisUtil jedis = JedisUtil.getInstance();
		Jedis jedis = null;
		jedis = jedisPool.getResource();
		String s = jedis.get("a");
		System.out.println(s);
		s=jedis.hget("aa", "b");
		System.out.println(s);
		jedis.close();
		
	/*	long start = System.currentTimeMillis();
		Set<String> set = new HashSet<>();
		for (int i = 0; i < 100000; i++) {
			
			int n = i;
			pushGdsLogTask(new Runnable() {
				
				@Override
				public void run() {
					Jedis jedis = null;
					try {
						jedis = jedisPool.getResource();
//						jedis.hget("a", "b");
						
						List<String> l = new ArrayList<>();
						for (int j = 0; j < 20; j++) {
							l.add(j+"");
						}
						
						jedis.hmget("1", l.toArray(new String[0]));
						
//						jedis.hget("1", n+"");
//						jedis.hset("1", ""+n, "[{\"flightArray\":[{\"flightCode\":\"CZ\",\"flightClass\":\"ALL\",\"flightType\":\"1\",\"carrierFlightCode\":\"\",\"applyType\":\"1\",\"applyFlightNo\":\"\",\"type\":\"go\",\"seq\":1},{\"flightCode\":\"CZ\",\"flightClass\":\"ALL\",\"flightType\":\"1\",\"carrierFlightCode\":\"\",\"applyType\":\"1\",\"applyFlightNo\":\"\",\"type\":\"back\",\"seq\":1}],\"godateArray\":[{\"offDateBegin\":\"2017-09-21\",\"offDateEnd\":\"2018-06-30\"}],\"backdateArray\":[],\"selldateArray\":[{\"sellDateBegin\":\"2017-09-21\",\"sellDateEnd\":\"2018-06-30\"},{\"sellDateBegin\":\"2017-09-21\",\"sellDateEnd\":\"2018-06-30\"},{\"sellDateBegin\":\"2017-09-21\",\"sellDateEnd\":\"2018-06-30\"},{\"sellDateBegin\":\"2017-09-21\",\"sellDateEnd\":\"2018-06-30\"}],\"fareBasis\":{\"fareBasisValue\":\"\",\"fareBasisType\":\"pre\"},\"transfer\":\"0\",\"excludeCity\":\"\",\"transferAirportCode\":\"\",\"passengerType\":\"0,1\",\"priceSectionBegin\":\"\",\"priceSectionEnd\":\"\",\"currency\":\"HKD\",\"priceType\":\"1\",\"priceAfterPublic\":\"4\",\"pricePublic\":\"0\",\"ticketPriceFormatPublic\":\"1\",\"ticketPricePublic\":\"0\",\"priceAfterPrivate\":\"0\",\"pricePrivate\":\"0\",\"ticketPriceFormatPrivate\":\"1\",\"ticketPricePrivate\":\"0\",\"pid\":2028,\"priority\":40,\"redisKey\":\"GLOBAL_IN_roundTrip\"}]");
					}catch (Exception e) {
						e.printStackTrace();
					}finally{
						if(jedis != null){
							jedis.close();
						}
					}
					
//					List<String> l = new ArrayList<>();
//					for (int j = 0; j < 20; j++) {
//						l.add(j+"");
//					}
//					try {
//						jedis.HASH.hmget("1", l.toArray(new String[0]));
//					} catch (Exception e) {
//						e.printStackTrace();
//					}
					
				}
			});
		}
		
		gdsThreadExecutor.shutdown();
		while (true) {
			if(gdsThreadExecutor.isTerminated()){
				System.out.println("时间消耗："+(System.currentTimeMillis() - start));
				System.out.println(set.toString());
				System.out.println("完成了");
				break;
			}
			try {
				Thread.sleep(100);
			} catch (InterruptedException e) {
				e.printStackTrace();
			}
		}*/
	}
	
	private static JedisResourcePool buildPool(){
		JedisPoolConfig pool = new JedisPoolConfig();
		pool.setMinIdle(200);
		pool.setMaxIdle(200);
		pool.setMaxTotal(800);
		pool.setMaxWaitMillis(3000);
		pool.setMinEvictableIdleTimeMillis(60000);
		pool.setSoftMinEvictableIdleTimeMillis(60000);
		pool.setTimeBetweenEvictionRunsMillis(1800000);
		pool.setNumTestsPerEvictionRun(1000);
		pool.setBlockWhenExhausted(false);
		pool.setTestOnBorrow(false);
		pool.setTestOnReturn(false);
		pool.setTestWhileIdle(true);
		
		return RoundRobinJedisPool
				.create()
				.curatorClient("47.92.92.167:2181", 30000)
				.zkProxyDir("/jodis/netquerycache")
				.poolConfig(pool)
				.database(0)
				.password("fxtc2018")
				.soTimeoutMs(30000)
				.build();
	}

}
